<!DOCTYPE html>
<html>
<head>
    <script type='text/javascript' src="./cordova.js"></script>
</head>

<body>
<p id="mdm_status">check mdm is active . . .</p>
<button id="active_mdm">activate mdm now</button>

<br/>
<button id="lock_screen">lock (require mdm active)</button>
<script type='text/javascript'>
    var mdmStatus = false;
    var statusInfo = document.getElementById("mdm_status");
    var stateActive = function () {
        statusInfo.innerHTML = "MDM is active";
        mdmStatus = true;
    };
    var stateInactive = function () {
        statusInfo.innerHTML = "MDM not active";
        mdmStatus = false;
    };

    // 检测激活
    var checkMmdActive = function () {
        cordova.exec(function (isActive) {
            console.log("check active, isActive=" + isActive);
            if ("true" === isActive) {
                stateActive();
            } else {
                stateInactive();
            }
        }, stateInactive, "mdm", "check_active", "");
    }

    window.onload = function () {
        checkMmdActive();
    };

    // 激活
    document.getElementById("active_mdm").onclick = function () {
        if (!mdmStatus) {
            cordova.exec(checkMmdActive(), stateInactive, "mdm", "active", "");
        }
    };

    // 执行指令
    document.getElementById("lock_screen").onclick = function () {
        if (mdmStatus) {
            // 注意 cmd中的 单引号 双引号，不可互换
            var cmd = "{'type': 'mdm','content': {'lock_screen': 'aa'}}";
            cordova.exec(stateActive, stateInactive, "mdm", "command", cmd);
        }
    };

</script>
</body>
</html>